package com.tanhua.dubbo.api;

import com.mongodb.client.result.DeleteResult;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.*;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class VideoCommentApiImpl implements VideoCommentApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean hasComment(String videoId, Long userId, CommentType like) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(videoId))
                .and("commentType").is(like.getType());
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query, VideoComment.class); //判断数据是否存在
    }

    @Override
    public Integer save(VideoComment videoComment) {
        //1、查询动态
        Video video = mongoTemplate.findById(videoComment.getPublishId(), Video.class);
        //2、向comment对象设置被评论人属性
        if (video != null) {
            videoComment.setPublishUserId(video.getUserId());
        }
        //3、保存到数据库
        mongoTemplate.save(videoComment);
        //4、更新动态表中的对应字段
        Query query = Query.query(Criteria.where("id").is(videoComment.getPublishId()));
        Update update = new Update();
        if (videoComment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else if (videoComment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//获取更新后的最新数据
        Video modify = mongoTemplate.findAndModify(query, update, options, Video.class);
        //5、获取最新的评论数量，并返回
        return modify.statisCount(videoComment.getCommentType());
    }

    @Override
    public void delete(VideoComment videoComment) {
        //1、删除Comment表数据
        ObjectId publishId = videoComment.getPublishId();
        DeleteResult publishId1 = mongoTemplate.remove(Query.query(Criteria.where("publishId").is(publishId)), VideoComment.class);
        //2、修改动态表中的总数量
        Query movementQuery = Query.query(Criteria.where("id").is(videoComment.getPublishId()));
        Update update = new Update();
        if (videoComment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else if (videoComment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", -1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//获取更新后的最新数据
        Movement modify = mongoTemplate.findAndModify(movementQuery, update, options, Movement.class);
    }

    /**
     * 分页查询评论列表
     *
     * @param id
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public List<VideoComment> findComments(String id, Integer page, Integer pagesize) {
        //1、构造查询条件
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(id)).and("commentType").is(2))
                .skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        //2、查询并返回
        return mongoTemplate.find(query, VideoComment.class);
    }

    @Override
    public VideoComment find(String commentId) {
        return  mongoTemplate.findById(new ObjectId(commentId),VideoComment.class);

    }


//    @Override
//    public VideoComment findById(String commentId) {
//        VideoComment videoComment = mongoTemplate.findById(commentId, VideoComment.class);
//        return videoComment;
//    }

//    @Override
//    public void updateById(String commentId) {
//        try {
//            VideoComment videoComment = this.findById(commentId);
//            //、修改评论表中的评论点赞数量
//            Query query = Query.query(Criteria.where("_id").is(commentId));
//            Update update = new Update();
//            if (videoComment.getCommentType() == CommentType.COMMENT.getType()) {
//                update.inc("likeCount", 1);
//            }
//            //设置更新参数
//            FindAndModifyOptions options = new FindAndModifyOptions();
//            options.returnNew(true);//获取更新后的最新数据
//            mongoTemplate.findAndModify(query, update, options, VideoComment.class);
//        } catch (Exception exception) {
//            exception.printStackTrace();
//        }
//    }


}
